package cn.linstudy.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import cn.linstudy.product.entity.ProductAttrValueEntity;
import cn.linstudy.product.service.ProductAttrValueService;
import cn.linstudy.product.vo.AttrRespVo;
import cn.linstudy.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.linstudy.product.entity.AttrEntity;
import cn.linstudy.product.service.AttrService;
import cn.linstudy.common.utils.PageUtils;
import cn.linstudy.common.utils.R;



/**
 * 商品属性
 *
 * @author XiaoLin
 * @email ljp2020vip@163.com
 * @date 2023-01-04 15:50:41
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data", productAttrValueEntityList);
    }

    @GetMapping("/base/list/{catalogId}")
    public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("catalogId") Long catalogId){
        PageUtils page = attrService.queryBaseAttrListPage(params,catalogId);
        return R.ok().put("page", page);
    }

    /**
     *  查询销售属性
     * @param params
     * @param catalogId
     * @return
     */
    @GetMapping("/sale/list/{catalogId}")
    public R saleTypeBaseAttrList(@RequestParam Map<String,Object> params,@PathVariable("catalogId") Long catalogId){
        PageUtils page = attrService.querySaleBaseAttrListPage(params,catalogId);
        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);
        return R.ok();
    }

    @RequestMapping("/update/{spuId}")
    public R updateBySpuId(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> productAttrValueEntityList){
        productAttrValueService.updateSpuAttrBySpuId(spuId,productAttrValueEntityList);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
